package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.constant.CommonConstant;
import com.huaxiafinance.management.dto.request.UnregisterDto;
import com.huaxiafinance.management.mapper.MemberInfoUnregisterMapper;
import com.huaxiafinance.management.mapper.YfqDictionaryMapper;
import com.huaxiafinance.management.model.MemberInfoUnregister;
import com.huaxiafinance.management.service.UnregisterService;
import com.huaxiafinance.management.util.DateUtil;
import com.huaxiafinance.management.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("SpringJavaAutowiringInspection")
@Transactional
public class UnregisterServiceImpl implements UnregisterService {

    @Autowired
    private MemberInfoUnregisterMapper memberInfoUnregisterMapper;

    @Autowired
    private YfqDictionaryMapper yfqDictionaryMapper;

    /**
     * 好友邀请查询列表
     *
     * @param dto 查询请求参数
     * @return
     */
    @Override
    public MessageResult queryUnregister(UnregisterDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        int sum = 0;
        //计算查询时间
        if (dto.getStartTime()!=null&&dto.getEndTime()!=null
                &&!("").equals(dto.getStartTime())
                &&!("").equals(dto.getEndTime())){
            Map<String, Date> dateMap = DateUtil.getTimeFormat(dto.getStartTime(), dto.getEndTime());
            dto.setStartDate(dateMap.get("beginTime"));
            dto.setEndDate(dateMap.get("overTime"));
        }else {
            dto.setStartTime(null);
            dto.setEndTime(null);
        }
        dto.setPage(dto.getPage() * dto.getRows());
        Map<String,Object> map = new HashMap<String,Object>();
        //查询订单列表
        List<MemberInfoUnregister> list = memberInfoUnregisterMapper.queryByUnregisterDto(dto);
        //计算总数
        sum = memberInfoUnregisterMapper.countUnregister(dto);
        //统计邀请人数
        int inviteNum = memberInfoUnregisterMapper.countInviteNum(dto);
        //统计注册人数
        int unregisterNum = memberInfoUnregisterMapper.countUnregisterNum(dto);
        map.put("list",list);
        map.put("inviteNum",inviteNum);
        map.put("unregisterNum",unregisterNum);
        messageResult.setData(map);
        messageResult.setSuccessResult();
        messageResult.setRows(sum);
        return messageResult;
    }

    /**
     * 邀请渠道列表
     *
     * @return
     */
    @Override
    public MessageResult queryChannelList() throws Exception {
        MessageResult messageResult = new MessageResult();
        List<Map<String, Object>> map = yfqDictionaryMapper.selectDictList(CommonConstant.CHANNEL_TYPE);
        messageResult.setData(map);
        messageResult.setSuccessResult();
        return messageResult;
    }


}
